package version_info

import (
	"github.com/gin-gonic/gin"
	"tao-admin/configs"
	"tao-admin/internal/entity"
	model2 "tao-admin/internal/model"
	"tao-admin/utils/copier"
)

type VersionManager struct{}

type SearchVersionCondition struct {
	VersionName        *string `json:"version_name"`
	VersionId          *string `json:"version_id"`
	VersionDescription *string `json:"version_description"`
}

func (manager *VersionManager) SearchVersionByConditionMng(ctx *gin.Context, condition *SearchVersionCondition) ([]*entity.VersionInfoEntity, int64, error) {
	db := configs.GetGormDB()
	versionList := make([]model2.VersionTab, 0)
	var total int64 = 0

	if condition.VersionName != nil {
		db = db.Where("version_name=?", condition.VersionName)
	}

	err1 := db.Model(&model2.OnDutyTab{}).Count(&total).Error
	db = db.Order("project DESC, type ASC")
	err2 := db.Find(&versionList).Error
	if err1 != nil || err2 != nil {
		return nil, 0, nil
	}

	outVersionList := make([]*entity.VersionInfoEntity, 0)
	copyErr := copier.Copy(versionList, &outVersionList)
	if copyErr != nil {
		return nil, 0, copyErr
	}
	return outVersionList, total, nil
}
